3
const data = [
  {
    key: "yi",
    name: "翌",
    birthday: "20220517",
    lunar: false,
  },
  {
    key: "xiu",
    name: "秀",
    birthday: "19930119",
    lunar: true,
  },
  {
    key: "xin",
    name: "鑫",
    birthday: "19931206",
    lunar: true,
  },
];

const item = getNextItem()

const diffDay = getDiffDay(item.time)

const widget = new ListWidget()
const nameContainer = widget.addStack()
nameContainer.size = new Size(32, 32)
nameContainer.backgroundColor = Color.white()
nameContainer.cornerRadius = 32

const name = nameContainer.addText(item.name)
name.textColor = new Color('#058527', 0.5)
name.font = Font.boldRoundedSystemFont(20)
name.centerAlignText()

nameContainer.centerAlignContent()

widget.addSpacer()

const dayContainer = widget.addStack()
const count = dayContainer.addText(diffDay.toString())
count.minimumScaleFactor = 0.5
const cFont = new Font('AvenirNext-HeavyItalic', 68)
count.font = cFont
count.textColor = Color.white()


const day = dayContainer.addText('day')
const dFont = new Font('AvenirNext-HeavyItalic', 16)
day.font = dFont
day.textColor = new Color("#000000", 0.5)


dayContainer.bottomAlignContent()
dayContainer.layoutHorizontally()



widget.backgroundColor = Color.orange()
// widget.backgroundImage = generateImage()
widget.presentSmall()
Script.setWidget(widget)



function generateImage() {


  const cake = SFSymbol.named('birthday.cake')
  const cakeImg = cake.image
  const ctx = new DrawContext()
//   DrawContext.opaque=false
  ctx.size = new Size(158,158)
  ctx.setFillColor(Color.orange())
const rect = new Rect(0, 0, 158, 158)
  ctx.fill(rect)
  ctx.drawImageInRect(cakeImg, new Rect(50,10,100,100))
ctx.setStrokeColor(Color.green())
ctx.stroke(rect)
  const img = ctx.getImage()
  return img
}


// 获取最近的生日
function getNextItem() {
  const currentDate = new Date();
  const currentYear = currentDate.getFullYear();
  const currentTime = currentDate.getTime()

  let lastestItem = null;

  const dateFormat = new DateFormatter();
  dateFormat.dateFormat = "yyyyMMdd";
  data.forEach((item) => {
    let nextBirthday = currentYear + item.birthday.slice(4);

    if (item.lunar) {
      // 农历转阴历
      const lunarJs = importModule("lunar.js");
      const lunar = lunarJs.Lunar.fromYmd(
        currentYear,
        item.birthday.slice(4, 6),
        item.birthday.slice(6)
      )
      const lunarString = lunar.getSolar().toString();
      nextBirthday = currentYear + lunarString.replaceAll("-", "").slice(4);
    }
    let date = dateFormat.date(nextBirthday);
    let time = date.getTime();

    // 今年已经过完了
    if (currentTime > time) {
      nextBirthday = (currentYear + 1) + nextBirthday(4)
      date = dateFormat.date(nextBirthday);
      time = date.getTime();
    }

    item.time = time;
    if (lastestItem) {
      if (item.time < lastestItem.time) {
        lastestItem = item;
      }
    } else {
      lastestItem = item;
    }
  });

  return lastestItem;
}

function getDiffDay(time) {
  const date = new Date();
  const currentTime = date.getTime();
  const diff = time - currentTime;
  const days = parseInt(diff / (1000 * 60 * 60 * 24));
  return days;
}
